package serialize;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;

import javax.persistence.Transient;

public class SerializableDemo {
	static class User implements Serializable{
		/**
		 * 
		 */
		private static final long serialVersionUID = -4070949070094423232L;
		String name;
		 String lastName;
//		private static final ObjectStreamField[] serialPersistentFields = { 
//		    new ObjectStreamField("lastName", String.class) 
//		}; 
		protected User(String name, String lastName) {
			this.name = name;
			this.lastName = lastName;
		}
		
		public String toString() {
			return this.name + " " + this.lastName;
		}
		
		private void writeObject(ObjectOutputStream output) throws IOException {
		    output.defaultWriteObject();
		    output.writeUTF("Hello World");
		}
		
		private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
		    input.defaultReadObject();
		}  
	}
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		File f = new File("/");
		System.out.println(f.exists());
		User u = new User("wood", "zhang");
		ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("serializable.txt"));
		os.writeObject(u);
		
		ObjectInputStream is = new ObjectInputStream(new FileInputStream("serializable.txt"));
		User us = (User) is.readObject();
		System.out.println(us);
	}
}
